from bdtime import tt
# import pyautogui
# import pydirectinput


# allWindows = pyautogui.getAllWindows()
#
#
# ## 打印所有活动窗口
# for win32Window in allWindows:
#     if win32Window.title == '原神':
#         target_window: pyautogui.Window = win32Window
#     print("打印所有活动窗口:", win32Window)
#
#
# target_window._getWindowRect()
# target_window.left = 0
# target_window.top= 0


import keyboard
import mouse
import pyautogui
import ctypes
import pydirectinput
from tools import is_left_mouse_button_pressed


player = ctypes.windll.kernel32


is_recurrent_talking = False
is_recurrent_walking = False
is_recurrent_collecting = False
is_recurrent_rotate = False
is_recurrent_rotate_forever = False


def _rotate_mouse(rotation_times=28, rotation_angle=900, rotation_duration=0.01, rotation_direction=1):
    global is_recurrent_rotate
    for i in range(rotation_times):
        if not is_recurrent_rotate or tt.stop('s'):
            break
        # tt.sleep(1)
        pydirectinput.moveRel(xOffset=rotation_direction * rotation_angle, yOffset=0, duration=rotation_duration * sleep_time_scale, relative=True)

    if not is_recurrent_rotate_forever:
        is_recurrent_rotate = False
        print('~~~ _rotate_mouse end')


def change_is_recurrent_rotate_forever_flag():
    global is_recurrent_rotate_forever
    is_recurrent_rotate_forever = not is_recurrent_rotate_forever
    print('~~~``` is_recurrent_rotate_forever:', is_recurrent_rotate_forever)
    beep()


def change_is_recurrent_rotate_flag():
    global is_recurrent_rotate
    if is_recurrent_rotate is False and not is_left_mouse_button_pressed():
        print('--- not pressed left mouse!')
        return

    is_recurrent_rotate = not is_recurrent_rotate
    print('~~~ change_is_recurrent_rotate_flag:', is_recurrent_rotate)
    beep()


click_positions = [
    [1333, 733],
    [1333, 800]
]


def beep(v=800, t=100):
    player.Beep(v, t)


beep()


def press_and_release(key, interval=0.1):
    keyboard.press(key)
    tt.sleep(interval)
    keyboard.release(key)


is_press_shift = False


def change_is_recurrent_collecting_flag():
    beep()
    # global is_recurrent_collecting
    # is_recurrent_collecting = False

    global is_recurrent_collecting
    is_recurrent_collecting = not is_recurrent_collecting
    if is_recurrent_collecting:
        keyboard.press('f')
        tt.sleep(0.3)
        if is_press_shift:
            press_and_release('shift', 1)
    else:
        keyboard.release('f')
    print('~~~ change_is_recurrent_collecting_flag:', is_recurrent_collecting)


def stop_talking_and_walking_flag():
    global is_recurrent_walking, is_recurrent_talking
    if is_recurrent_talking == True or is_recurrent_walking == True:
        beep()
    is_recurrent_talking = False
    is_recurrent_walking = False


def change_is_recurrent_walking_flag():
    beep()
    global is_recurrent_talking
    is_recurrent_talking = False

    global is_recurrent_walking
    is_recurrent_walking = not is_recurrent_walking
    if is_recurrent_walking:
        keyboard.press('w')
        tt.sleep(0.3)
        if is_press_shift:
            press_and_release('shift', 1)
    else:
        keyboard.release('w')
    print('~~~ change_is_recurrent_walking_flag:', is_recurrent_walking)


def change_is_recurrent_talking_flag():
    beep(600)

    global is_recurrent_walking
    keyboard.release('w')
    is_recurrent_walking = False

    global is_recurrent_talking
    is_recurrent_talking = not is_recurrent_talking

    # event = keyboard.read_event()
    # print('event --- ', event)
    # if event.event_type == keyboard.KEY_DOWN and event.name == 'space':
    #     print('space was pressed')

    print('~~~ change_is_recurrent_flag:', is_recurrent_talking)


from threading import Thread


flag__exist_talking_task = False  # 谈话任务
flag__exist_walking_task = False  # 一直按w向前的任务


# def _start_talking_task():
#     global is_recurrent_walking, flag__exist_walking_task
#     print('===start _start_talking_task, is_recurrent_walking:', is_recurrent_walking)
#
#     break_flag = False
#     while True:
#         while is_recurrent_walking:
#             if tt.stop_alt('s'):
#                 break_flag = True
#                 break
#
#
#
#         if break_flag:
#             break
#
#     flag__exist_walking_task = False
#     print('*** end _start_talking_task, is_recurrent_walking:', is_recurrent_walking)


def _start_talking_task():
    global is_recurrent_talking, flag__exist_talking_task
    print('===start start_talking_task, is_recurrent:', is_recurrent_talking)

    break_flag = False
    i = 0
    last_t_ls = [-1, -1, -1]
    while True:
        i += 1
        if tt.stop_alt('s'):
            break_flag = True
            break

        if is_recurrent_rotate:
            _rotate_mouse()
            tt.sleep(0.1)

        if is_recurrent_collecting:
            press_and_release('f', 0.1)
            tt.sleep(0.1)

        if is_press_shift:
            while is_recurrent_walking:
                if tt.now(0) % 6 == 0:
                    keyboard.press_and_release('shift')

        if is_recurrent_click:
            pyautogui.leftClick()
            tt.sleep(0.1 * sleep_time_scale)

            now_0 = tt.now(0)
            if now_0 % 4 == 0 and now_0 > last_t_ls[0]:
                press_and_release('e')
                last_t_ls[0] = now_0
            if now_0 % 5 == 0 and now_0 > last_t_ls[1]:
                press_and_release('shift')
                last_t_ls[1] = now_0
            if now_0 % 13 == 0 and now_0 > last_t_ls[1]:
                press_and_release('q')
                last_t_ls[2] = now_0

        while is_recurrent_talking:
            if tt.stop_alt('s'):
                break_flag = True
                break

            print('--- on start_talking_task')
            # x, y = mouse.get_position()
            # mouse.move(0, 0)
            for x, y in click_positions:
                # x, y = 1240, 537
                mouse.move(x, y)
                # pyautogui.leftClick()
                pyautogui.click(x, y, interval=0.06)
                tt.sleep(0.1)
                # mouse.click(mouse.LEFT)

            mouse.move(1920, 1280)
            tt.sleep(3 * sleep_time_scale)

        if break_flag:
            break
        tt.sleep(0.01)

    flag__exist_talking_task = False
    print('*** end start_talking_task, is_recurrent:', is_recurrent_talking)


def start_talking_task():
    global flag__exist_talking_task

    if flag__exist_talking_task:
        return

    flag__exist_talking_task = True
    task = Thread(target=_start_talking_task)
    task.start()


# recorded = mouse.record()
#
# i = 0
# while is_recurrent:
#     if tt.stop_alt('s'):
#         print('--- i:', i)
#         break
#
#     mouse.replay(recorded)
#
#     i += 1
#     print('--- i:', i)

# Block forever, like `while True`.
# mouse.wait()

# keyboard.record(until='esc')
# keyboard.play()


is_recurrent_click = False


def change_recurrent_click_flag():
    global is_recurrent_click
    beep()
    is_recurrent_click = not is_recurrent_click


sleep_time_ls = [0.33, 0.66, 1]
sleep_time_index = 0
sleep_time_scale = sleep_time_ls[sleep_time_index]


def turn_sleep_time_scale():
    beep()

    global sleep_time_index, sleep_time_scale
    sleep_time_index = (sleep_time_index + 1) % len(sleep_time_ls)
    if sleep_time_index == 0:
        beep()

    sleep_time_scale = sleep_time_ls[sleep_time_index]
    print('--- turn_sleep_time_scale:', sleep_time_scale)


keyboard.add_hotkey('w', stop_talking_and_walking_flag)
keyboard.add_hotkey('s', stop_talking_and_walking_flag)
keyboard.add_hotkey('alt + a', change_is_recurrent_walking_flag)
keyboard.add_hotkey('alt + d', change_is_recurrent_talking_flag)
keyboard.add_hotkey('alt + f', change_is_recurrent_collecting_flag)
keyboard.add_hotkey('alt + e', change_recurrent_click_flag)
# keyboard.add_hotkey('alt + d', start_talking_task)
start_talking_task()

keyboard.add_hotkey('alt + r', change_is_recurrent_rotate_forever_flag)

keyboard.add_hotkey('alt + t', turn_sleep_time_scale)


while True:
    # Wait for the next event.
    try:
        event = keyboard.read_event()
        if event.event_type == keyboard.KEY_DOWN and event.name == 'r':
            change_is_recurrent_rotate_flag()
    except Exception as e:
        print('*** error:', e)

# Blocks until you press esc.
# keyboard.wait('esc')


